package com.example.analysis.utils;

import com.example.analysis.entity.Quest;
import com.example.analysis.vo.ReliabilityItemVO;
import com.example.analysis.vo.ReliabilityVO;
import java.util.ArrayList;
import java.util.List;

public class ReliabilityAnalysis {
    public static ReliabilityVO analysis(double[][] rawData, List<Quest> questList) {
        double alpha = calcAlpha(rawData);
        double[][] ret = calcCorrelations(rawData);
        double[] correlations = ret[0];
        double[] removedAlphas = ret[1];
        // 封装
        List<ReliabilityItemVO> list = new ArrayList<>();
        for (int i = 0; i < rawData.length; i++) {
            ReliabilityItemVO item = new ReliabilityItemVO(questList.get(i).getQuestEvaluate(), correlations[i], removedAlphas[i]);
            list.add(item);
        }
        return new ReliabilityVO(alpha, list);
    }

    // 计算问卷总分
    private static double[] calcTotal(double[][] data) {
        int questNum = data[0].length;
        double[] total = new double[questNum];
        for (double[] tmp : data) {
            for (int s = 0; s < questNum; s++) {
                total[s] += tmp[s];
            }
        }
        return total;
    }

    // 计算α
    private static double calcAlpha(double[][] data) {
        double[] total = calcTotal(data);
        double varianceSum = 0, totalVariance = ComCalcUtils.calcVariance(total);
        for (double[] itemScores : data) {
            double itemVariance = ComCalcUtils.calcVariance(itemScores);
            varianceSum += itemVariance;
        }
        int k = data.length;
        return (double) k / (k - 1) * (1 - varianceSum / totalVariance);
    }

    // 计算校正项总计相关性（CITC）
    private static double[][] calcCorrelations(double[][] data) {
        double[] correlationList = new double[data.length];
        double[] removedAlphaList = new double[data.length];
        for (int i = 0; i < data.length; i++) {
            double[] itemScores = data[i];
            double[] totalWithoutItem = totalWithoutItem(data, i);
            double correlation = ComCalcUtils.calcPearson(itemScores, totalWithoutItem);
            double removedAlpha = calcRemovedAlpha(data, i);
            correlationList[i] = correlation;
            removedAlphaList[i] = removedAlpha;
            System.out.println("i = " + i + ", correlation = " + correlation + ", removedAlpha = " + removedAlpha);
        }
        return new double[][]{correlationList, removedAlphaList};
    }

    // 计算排除某题的总分
    private static double[] totalWithoutItem(double[][] data, int idx) {
        int questNum = data[0].length;
        double[] total = new double[questNum];
        for (int q = 0; q < data.length; q++) {
            if (q == idx) continue;
            for (int s = 0; s < questNum; s++) {
                total[s] += data[q][s];
            }
        }
        return total;
    }


    // 项已删除的α系数
    private static double calcRemovedAlpha(double[][] data, int idx) {
        double[] total = totalWithoutItem(data, idx);
        double varianceSum = 0, totalVariance = ComCalcUtils.calcVariance(total);
        for (int i = 0; i < data.length; i++) {
            if (i == idx)
                continue;
            double itemVariance = ComCalcUtils.calcVariance(data[i]);
            varianceSum += itemVariance;
        }
        int k = data.length;
        return (double) k / (k - 1) * (1 - varianceSum / totalVariance);
    }
}
